from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Attach(models.Model):
    """
    Attach can be used as custom field for other class
    """
    label = models.CharField(max_length=100)
    content = models.TextField()

class Class(models.Model):
    code = models.CharField(max_length=20, blank=True)
    name = models.CharField(max_length=50)
    students = models.ManyToManyField(User, related_name="cls")
    attachs = models.ManyToManyField(Attach, related_name="cls")
    
    def __unicode__(self):
        return self.name

class Course(models.Model):
    """
    syllabus: the outline of course
    """
    code = models.CharField(max_length=20, blank=True)
    name = models.CharField(max_length=50)
    attachs = models.ManyToManyField(Attach, related_name="course")
    
    def __unicode__(self):
        return self.name

class Lesson(models.Model):
    course = models.ForeignKey(Course, related_name="lessons")
    code = models.CharField(max_length=20, blank=True)
    teacher = models.ForeignKey(User, related_name="lessons")
    clses = models.ManyToManyField(Class, related_name="lessons")
    attachs = models.ManyToManyField(Attach, related_name="lesson")
    
    def __unicode__(self):
        return self.course.name + " " + self.teacher.username

class ActiveType(models.Model):
    label = models.CharField(max_length=50)
    attachs = models.ManyToManyField(Attach, related_name="active_type")
    
class ActiveTypeAtom(models.Model):
    active_type = models.ForeignKey(ActiveType, related_name="atoms")
    label = models.CharField(max_length=50, blank=True)
    atom = models.IntegerField()

class AtomHandler(models.Model):
    atom = models.ForeignKey(ActiveTypeAtom, related_name="handlers")
    handler = models.IntegerField()
    
class Active(models.Model):
    type = models.ForeignKey(ActiveType, related_name="actives")
    lesson = models.ForeignKey(Lesson, related_name="actives")
    name = models.CharField(max_length=50)
    create_time = models.DateTimeField(auto_now_add=True)
    attachs = models.ManyToManyField(Attach, related_name="active")

class Test(models.Model):
    active = models.ForeignKey(Active, related_name="test")
    atom = models.ForeignKey(ActiveTypeAtom, related_name="test")
    oj = models.PositiveIntegerField()
    contest_id = models.CharField(max_length=20)

class Status(models.Model):
    test = models.ForeignKey(Test, related_name="status")
    s_id = models.CharField(max_length=20)
    user_id = models.CharField(max_length=20)
    prob_id = models.CharField(max_length=20)
    result = models.IntegerField()
    mem = models.IntegerField()
    time = models.IntegerField()
    lang = models.IntegerField()
    len = models.IntegerField()
    sub_time = models.DateField()
    
    